package json;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.testng.annotations.Test;

/**
 * @author topjoy
 * json——对象转换
 * t1   map转json
 * t2   json转map
 */
public class ParseJSON {

    String s = "[{\"type\":\"2\",\"code\":\"92\",\"amount\":5},{\"type\":\"2\",\"code\":\"706032\",\"amount\":5}]";




    @Test
    private void t1() throws FileNotFoundException {
        var map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 2);

        String s = JSONObject.toJSONString(map);
        System.out.println(s);

        System.out.println(JSONObject.parseObject(s, HashMap.class));
    }

    @Test
    private void t2() {
        String str = "[{\"achieveTotal\":0,\"content\":\"{\\\"links\\\":{},\\\"txt\\\":\\\"发言测试二\\\",\\\"type\\\":1}\",\"headFrame\":\"1102001\",\"headImg\":\"1101025\",\"hornMsg\":0,\"index\":2,\"lv\":32,\"nickname\":\"宇文之柔12\",\"rid\":\"22603_3\",\"sendTime\":1662485248019,\"unionName\":\"春天到了\",\"unionTitleTag\":\"1\"},{\"achieveTotal\":0,\"content\":\"{\\\"links\\\":{},\\\"txt\\\":\\\"发芽测试\\\",\\\"type\\\":1}\",\"headFrame\":\"1102001\",\"headImg\":\"1101025\",\"hornMsg\":0,\"index\":1,\"lv\":32,\"nickname\":\"宇文之柔12\",\"rid\":\"22603_3\",\"sendTime\":1662485223759,\"unionName\":\"春天到了\",\"unionTitleTag\":\"1\"}]";
        String str2 = "{\"4\":[{\"achieveTotal\":0,\"content\":\"{\\\"links\\\":{},\\\"txt\\\":\\\"发言测试二\\\",\\\"type\\\":1}\",\"headFrame\":\"1102001\",\"headImg\":\"1101025\",\"hornMsg\":0,\"index\":2,\"lv\":32,\"nickname\":\"宇文之柔12\",\"rid\":\"22603_3\",\"sendTime\":1662485248019,\"unionName\":\"春天到了\",\"unionTitleTag\":\"1\"},{\"achieveTotal\":0,\"content\":\"{\\\"links\\\":{},\\\"txt\\\":\\\"发芽测试\\\",\\\"type\\\":1}\",\"headFrame\":\"1102001\",\"headImg\":\"1101025\",\"hornMsg\":0,\"index\":1,\"lv\":32,\"nickname\":\"宇文之柔12\",\"rid\":\"22603_3\",\"sendTime\":1662485223759,\"unionName\":\"春天到了\",\"unionTitleTag\":\"1\"}]}";
        String str3 = "{\"4\":[{\"achieveTotal\":0,\"extra\":{\"unionId\":\"300001393\",\"nickName\":\"\",\"chatEdition\":1,\"id\":\"Kof_4112_3\",\"notify\":\"诸侯争霸初赛将在<color=#ffbe4d>5</color>分钟后开始，请主公不要忘记参赛哟\",\"minute\":5},\"hornMsg\":0,\"index\":5,\"lv\":0,\"sendTime\":1662551100512},{\"achieveTotal\":0,\"extra\":{\"unionId\":\"300001393\",\"nickName\":\"\",\"chatEdition\":1,\"id\":\"Kof_4112_2\",\"notify\":\"诸侯争霸初赛将在<color=#ffbe4d>15</color>分钟后开始，请主公不要忘记参赛哟\",\"minute\":15},\"hornMsg\":0,\"index\":4,\"lv\":0,\"sendTime\":1662550500513},{\"achieveTotal\":0,\"extra\":{\"unionId\":\"300001393\",\"nickName\":\"\",\"chatEdition\":1,\"id\":\"Kof_4112_1\",\"notify\":\"诸侯争霸初赛将在<color=#ffbe4d>30</color>分钟后开始，请主公不要忘记参赛哟\",\"minute\":30},\"hornMsg\":0,\"index\":3,\"lv\":0,\"sendTime\":1662549600522},{\"achieveTotal\":0,\"content\":\"{\\\"links\\\":{},\\\"txt\\\":\\\"发言测试二\\\",\\\"type\\\":1}\",\"headFrame\":\"1102001\",\"headImg\":\"1101025\",\"hornMsg\":0,\"index\":2,\"lv\":32,\"nickname\":\"宇文之柔12\",\"rid\":\"22603_3\",\"sendTime\":1662485248019,\"unionName\":\"春天到了\",\"unionTitleTag\":\"1\"},{\"achieveTotal\":0,\"content\":\"{\\\"links\\\":{},\\\"txt\\\":\\\"发芽测试\\\",\\\"type\\\":1}\",\"headFrame\":\"1102001\",\"headImg\":\"1101025\",\"hornMsg\":0,\"index\":1,\"lv\":32,\"nickname\":\"宇文之柔12\",\"rid\":\"22603_3\",\"sendTime\":1662485223759,\"unionName\":\"春天到了\",\"unionTitleTag\":\"1\"}]}";

        JSONObject jsonObject = JSONObject.parseObject(str3);
        // get得到的数组在转换的时候有时候会出现类型转换错误①，
        JSONArray jsonArray = jsonObject.getJSONArray("4");

//        var filter = jsonArray.stream()
//            // ①如果jsonArray是通过getJSONArray得到，这里的转换可能会出现class Xxx cannot be cast to class JSONObject的问题。解决办法是通过解析（parseArray）得到jsonArray
//            .map(i -> (JSONObject)i)
//            .filter(i -> {
//                JSONObject content = i.getJSONObject("content");
//                return Objects.nonNull(content) && StringUtils.isNotEmpty(content.getString("txt"));
//            })
//            .collect(Collectors.toList());
//
//        jsonArray.clear();
//        jsonArray.addAll(filter);

//        String s = new JSONArray(Collections.singletonList(filter)).toJSONString();
//        JSONArray array2 = JSONArray.parseArray(s);

        // global
        JSONArray array2 = JSONArray.parseArray(jsonArray.toJSONString());


//        array2.stream()
//            .map(value -> (JSONObject)value)
//            .forEach(o -> {
//                JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
//                JSONObject content = jsonObject1.getJSONObject("content");
//                String txt = content.getString("txt");
//                o.put("content", txt);
//            });
//
//        array2.forEach(System.out::println);

//        jsonArray.forEach(i -> {
//            var object = (JSONObject) i;
//            //            JSONObject jsonObject1 = JSONObject.parseObject(i.toString());
//            JSONObject content = object.getJSONObject("content");
//            String txt = content.getString("txt");
//
//            object.put("content", txt);
//        });

//        jsonArray.forEach(i -> System.out.println(i));

//        JSONArray.parseArray(str).forEach(i -> System.out.println(i));
//        System.out.println(JSONArray.parseArray(str));
        //        Object parse = JSONArray.parse(str);

    }


}
